#!/usr/bin/env python
#
# Copyright (c) 2011 GhostBSD
#
# See COPYING for licence terms.
#
# gbi_partition.py v 0.0 Tuesday, February 22 2011 10:54:53 Eric
#
# gbi_partition.py create and delete partition slice for GhostBSD system.

import pygtk
pygtk.require('2.0')
import gtk, gobject
import os, os.path, sys, stat, shutil

class partitions:
    def close_application(self, widget):
        os.system("killall python")
    
    def quit_application(self, widget):
        gtk.main_quit()		
                
    def create_bbox(self, horizontal, spacing, layout):
        frame = gtk.Frame()
        if horizontal:
            bbox = gtk.HButtonBox()
        else:
            bbox = gtk.VButtonBox()
        bbox.set_border_width(5)
        frame.add(bbox)
        # Set the appearance of the Button Box
        bbox.set_layout(layout)
        bbox.set_spacing(spacing)
        button = gtk.Button(stock=gtk.STOCK_GO_BACK)
        bbox.add(button)
        button = gtk.Button(stock=gtk.STOCK_CANCEL)
        button.connect("clicked", self.close_application)
        bbox.add(button)
        button = gtk.Button(stock=gtk.STOCK_GO_FORWARD)
        bbox.add(button)
        button.connect("clicked", self.quit_application)
        return frame
    
    def create_labels(self, horizontal, spacing, layout):
        frame2 = gtk.Frame()
        if horizontal:
            bbox = gtk.HButtonBox()
        else:
            bbox = gtk.VButtonBox()
        bbox.set_border_width(5)
        frame2.add(bbox)
        # Set the appearance of the Button Box
        bbox.set_layout(layout)
        bbox.set_spacing(spacing)
        button = gtk.Button(stock=gtk.STOCK_CANCEL)
        bbox.add(button)
        button = gtk.Button(stock=gtk.STOCK_ADD)
        bbox.add(button)
        return frame2
    
    def on_clicked(self, window):
        window = gtk.Window()
        window.set_title("Create Slice")
        window.set_border_width(0)
        window.set_position(gtk.WIN_POS_CENTER)
        window.set_size_request(400, 150)
        try:
            window.set_icon_from_file("G-logo.png")
        except Exception, e:
            print e.message
            sys.exit(1)
        box1 = gtk.VBox(False, 0)
        window.add(box1)
        box1.show()
        box2 = gtk.VBox(False, 10)
        box2.set_border_width(10)
        box1.pack_start(box2, True, True, 0)
        box2.show()
        
        # create label 
        frame = gtk.Frame("Create New Partition Slice")
        self.table = gtk.Table(1, 2, True)       
        
        self.label1 = gtk.Label("New slice size in megabytes:")
        
        self.entry = gtk.SpinButton(adjustment=None, climb_rate=0.0, digits=0)
        

        self.table.attach(self.label1, 0, 1, 1, 2)
        self.table.attach(self.entry, 1, 2, 1, 2)
        
       
        frame.add(self.table)
        box2.pack_start(frame, False, False, 0)
        
        
        
        box2 = gtk.HBox(False, 10)
        box2.set_border_width(5)
        box1.pack_start(box2, False, True, 0)
        box2.show()

        # Add button  
        box2.pack_start(self.create_labels(True,
                                         10, gtk.BUTTONBOX_END),
                        True, True, 5)
        window.show_all ()
    
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("destroy", self.close_application)
        window.set_size_request(550, 400)  
        window.set_title("GBI Partition Slice")
        window.set_border_width(0)
        window.set_position(gtk.WIN_POS_CENTER)
        try:
            window.set_icon_from_file("G-logo.png")
        except Exception, e:
            print e.message
            sys.exit(1)
        box1 = gtk.VBox(False, 0)
        window.add(box1)
        box1.show()
        box2 = gtk.VBox(False, 10)
        box2.set_border_width(10)
        box1.pack_start(box2, True, True, 0)
        box2.show()
        
        # Chose disk part.   
        frame = gtk.Frame("Disk")
        self.table = gtk.Table(1, 2, True)
        self.label1 = gtk.Label("Please select the disk drive you whish to use:")
        self.disk = gtk.ComboBox()
        os.system("sh /GhostBSD/pc-sysinstall/pc-sysinstall disk-list")
        self.table.attach(self.label1, 0, 2, 0, 1)
        self.table.attach(self.disk, 0, 2, 1, 2)
        frame.add(self.table)
        box2.pack_start(frame, False, False, 0)  
        
        #Partition Slice
        frame = gtk.Frame("Partition Slice")
        self.table = gtk.Table(1, 2, True)       
        self.label = gtk.CheckButton("Use Entire Disk")
        self.sliced = gtk.ComboBox()        
        self.create = gtk.Button("Add Partition")
        self.create.set_size_request(80, 30)
        self.create.connect("clicked", self.on_clicked)
        self.delete = gtk.Button("Delete Partition")
        
        self.table.attach(self.label, 0, 1, 0, 1)
        self.table.attach(self.sliced, 0, 2, 1, 2)
        self.table.attach(self.delete, 0, 1, 2, 3)
        self.table.attach(self.create, 1, 2, 2, 3)
        frame.add(self.table)
        box2.pack_start(frame, False, False, 0)
        
        #auto partition or Cotumize Disk Partition.
        frame = gtk.Frame("Chose Option")
        self.table = gtk.Table(1, 2, True) 
        self.auto = gtk.RadioButton(None, "Auto Partition")
        self.costume = gtk.RadioButton(None, "Cotumize Disk Partition (Advanced)")
        self.boot = gtk.CheckButton("Install BSD Bootloader")
        
        self.table.attach(self.auto, 0, 1, 0, 1)
        self.table.attach(self.costume,1, 2, 0, 1)
        self.table.attach(self.boot,0, 1, 1, 2)
        frame.add(self.table)
        box2.pack_start(frame, False, False, 0)
        
        box2 = gtk.HBox(False, 10)
        box2.set_border_width(5)
        box1.pack_start(box2, False, True, 0)
        box2.show()

        # Add button  
        box2.pack_start(self.create_bbox(True,
                                         10, gtk.BUTTONBOX_END),
                        True, True, 5)
        window.show_all ()
        
def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    partitions()
    main()
